<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
class Index extends Pre
{
    //首页？
    public function index()
    {
        $from = $this -> search_word_from();
        if($from['from'] || $from['keyword']){
            cookie('words',['words'=>$from['keyword'],'origin'=>$from['from']],24*3600);
        }
        if(cookie('words')){
            $cook =  cookie('words');
            $this -> assign('words',$cook['words']);
            $this -> assign('origin',$cook['origin']);
        }
        //展示当前所有的海外房产
        $data = input('param.');
        if ($data['country']){
            switch ($data['country']){
                case 'MLXY' : $map[] = ['country','eq','马来西亚'];break;
                case 'JPZ' : $map[] = ['country','eq','柬埔寨'];break;
                case 'TG' : $map[] = ['country','eq','泰国'];break;
                case 'XL' : $map[] = ['country','eq','希腊'];break;
                case 'MG' : $map[] = ['country','eq','美国'];break;
                case 'ZG' : $map[] = ['country','eq','中国'];break;default;
            }
        }
        $allList = \app\common\model\FangBuildingListEnterlo::column('list,building_id');
        $map[] = ['list_enterlo','=',2];
        if($allList[2]){
            $exp = new \think\db\Expression("field(id,$allList[2])");
            $result = \app\common\model\FangBuilding::where($map)->order($exp)  -> select();
            foreach ($result as $key => $value){
                $img = $value['design_img'];
                $result[$key]['design_img'] = self::$url.$img;
            }
        }else{
            $result = '';
        }
        //展示当前所有国内旅居
        $map1[] = ['list_enterlo','=',1];
        if($allList[1]){
            $exp = new \think\db\Expression("field(id,$allList[1])");
            $result1 = \app\common\model\FangBuilding::where($map1)->order($exp)  -> select();
            foreach ($result1 as $key1 => $value1){
                $img1 = $value1['design_img'];
                $result1[$key1]['design_img'] = self::$url.$img1;
            }
        }else{
            $result1 = '';
        }
        $this -> assign('info',$result);
        $this -> assign('info1',$result1);

        $reportNum = \app\common\model\FangReport::where('date','>=',strtotime('2018-9-19'))->count();
        $this -> assign('reportNum',$reportNum+18);

        if (request()->isMobile()){
            return view();
        }
        else{
            return view('indexPC');
        }
    }

    static $url = 'https://show.enterlo.com';
    //详情页
    public function single()
    {
        $from = $this -> search_word_from();
        if (request()->isMobile()){
            $sign = '手机端';
        }else{
            $sign = '电脑端';
        }
        if($from){
            cookie('words',['words'=>$from['keyword'],'origin'=>$from['from']],24*3600);
        }
        if($from['from'] || $from['keyword']){
            $map['origin'] = $from['from']=='baidu'?'百度':$from['from'];
            $map['words'] = $from['keyword']==1?'信息流':$from['keyword'];
            $map['sign'] = $sign;
            $map['time'] = time();
            \app\common\model\Fangsearch::create($map);
        }
        $info = \app\common\model\FangBuilding::with('buildingDesc')->where('id',input('id'))->find();
        $type = explode(',',$info['type']);
        if (!$info)
            $this -> redirect('index/index');

        $data = input('param.');
        $telephone = '400-678-0522';
        //获取经纪人信息
        if ($data['account_id']){
            $accountInfo = \app\common\model\FangAccount::where('id',$data['account_id'])->find();
            //如果是全民经纪人,展示管理者的信息
            if ($accountInfo['role'] =='33'){
                $higher = $accountInfo -> higher ->a_id;
                $higherInfo = \app\common\model\FangAccount::where('id',$higher)->find();
                $accountInfo['telephone'] = $higherInfo['telephone'];
            }
            $this -> assign('accountInfo',$accountInfo);
            $telephone = $accountInfo['telephone'];
        }

        $this -> assign('telephone',$telephone);

        if ($type){
            $info = $info ->toArray();
            $buildingDesc = [];
            foreach ($type as $value){
                foreach ($info['building_desc'] as $k=> $item){
                    if ($value ==$item['id']){
                        $buildingDesc[] = $item;
                        unset($info['building_desc'][$k]);
                        break;
                    }
                }
            }
            foreach ($buildingDesc as $k => $value){
                $imgPre = explode(',',$value['img']);
                foreach ($imgPre as$j=> $item){
                    if ($item)
                        $imgPre[$j] = self::$url.$item;
                }
                $buildingDesc[$k]['img'] =$imgPre;
                if ($value['role'] =='video')
                    $buildingDesc[$k]['content'] = self::$url.$value['content'];
            }
            $info['building_desc'] = $buildingDesc;
        }

        $info['design_img'] = self::$url.$info['design_img'];
        $info['back_img'] = $info['back_img']?self::$url.$info['back_img']:null;
        $this -> assign('info',$info);

        $reportNum = \app\common\model\FangReport::where('date','>=',strtotime('2018-9-19'))->count();
        $this -> assign('reportNum',$reportNum+18);
        if(cookie('words')){
            $cook =  cookie('words');
            $this -> assign('words',$cook['words']);
            $this -> assign('origin',$cook['origin']);
        }
        if (request()->isMobile()){
            return view();
        }
        else{
            return view('singlePC');
        }
    }
    //页面展示
    public function search(){
        $list = \app\common\model\Fangsearch::order('id DESC') -> paginate(20);
        //今日点击总数量
        $ti = strtotime(date("Y-m-d",time()));
        $map[] = ['time','between',[$ti, $ti+86400]];
        $result = \app\common\model\Fangsearch::where($map) -> select();
        foreach ($result as $value){
            $tmp[] = $value['origin'];
            $key[] = $value['words'];
        }
        $t = array_filter(array_unique($tmp));
        $ke = array_filter(array_unique($key));
        foreach ($ke as $va){
            $ta = $ta.','.$va;
        }
        foreach ($t as $ve){
            $ba = $ba.','.$ve;
        }
        $time = count($result);
        $map[] = ['sign','eq','电脑端'];
        $computor = \app\common\model\Fangsearch::where($map) -> count();
        $map2[] = ['time','between',[$ti, $ti+86400]];
        $map2[] = ['sign','eq','手机端'];
        $mob = \app\common\model\Fangsearch::where($map2) -> count();
        $map5[] = ['time','between',[$ti, $ti+86400]];
        $map5[] = ['words','eq','信息流'];
        $xinxi = \app\common\model\Fangsearch::where($map5)->count();
        //昨日关键字
        $ti1 = strtotime(date("Y-m-d",time()));
        $mapg[] = ['time','between',[$ti1-86400, $ti1]];
        $guanjian = \app\common\model\Fangsearch::where($mapg) -> select();
        $guantime = count($guanjian);
        foreach ($guanjian as $val){
            $ke1[] = $val['words'];
        }
        $k = array_filter(array_unique($ke1));
        foreach ($k as $vu){
            $key1 = $key1.','.$vu;
        }
        //历史数据
        $result1 = \app\common\model\Fangsearch::all();
        $zong = count($result1);
        $map3[] = ['sign','eq','电脑端'];
        $com = \app\common\model\Fangsearch::where($map3) -> count();
        $map4[] = ['sign','eq','手机端'];
        $mob1 = \app\common\model\Fangsearch::where($map4) -> count();
        $this->assign('list', $list);
        $this->assign('tmp', ltrim($ba,','));
        $this->assign('key', ltrim($ta,','));
        $this->assign('zong', $time);
        $this->assign('computor', $computor);
        $this->assign('mob', $mob);
        $this->assign('zong1', $zong);
        $this->assign('com', $com);
        $this->assign('mob1', $mob1);
        $this->assign('xinxi', $xinxi);
        $this->assign('key1', ltrim($key1,','));
        return view();
    }
    //登录成功访问页面
    public function succeed(){
         return view();
    }
        //搜索关键字
    function search_word_from() {
        $referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
        if(strstr( $referer, 'baidu.com')){ //百度
            preg_match( "|baidu.+wo?r?d=([^\\&]*)|is", $referer, $tmp );
            $keyword = urldecode( $tmp[1] );
            $from = 'baidu';
    }elseif(strstr( $referer, 'google.com') or strstr( $referer, 'google.cn')){ //谷歌
            preg_match( "|google.+q=([^\\&]*)|is", $referer, $tmp );
            $keyword = urldecode( $tmp[1] );
            $from = 'google';
        }elseif(strstr( $referer, 'so.com')){ //360搜索
            preg_match( "|so.+q=([^\\&]*)|is", $referer, $tmp );
            $keyword = urldecode( $tmp[1] );
            $from = '360';
        }elseif(strstr( $referer, 'sogou.com')){ //搜狗
            preg_match( "|sogou.com.+query=([^\\&]*)|is", $referer, $tmp );
            $keyword = urldecode( $tmp[1] );
            $from = 'sogou';
        }elseif(strstr( $referer, 'soso.com')){ //搜搜
            preg_match( "|soso.com.+w=([^\\&]*)|is", $referer, $tmp );
            $keyword = urldecode( $tmp[1] );
            $from = 'soso';
        }else {
            $keyword ='';
            $from = '';
        }
        return array('keyword'=>$keyword,'from'=>$from);
    }

    public function ceshi(){
        $options = [
            'appid' => 'wx07ab75f91125067d',
            'appSecret' => '7622afecf8e869098316bc3764c6b407'
        ];
        $weObj = new \wechat\Jssdk($options['appid'],$options['appSecret']);
        //微信分享
        $wechat = $weObj -> GetSignPackage();
        dump($wechat);
    }
}
